<?php defined('InShopNC') or exit('Access Invalid!');?>

<div class="page">
  <div class="fixed-bar">
    <div class="item-title">
      <a class="back" href="index.php?act=marketing_activity&op=marketing_activity" title="返回活动列表"><i class="fa fa-arrow-circle-o-left"></i></a>
      <div class="subject">
        <h3><?php echo $lang['activity_index_manage'];?> - <?php echo $lang['activity_apply_manage'];?></a></h3>
        <h5><?php echo $lang['activity_apply_manage_subhead']; ?></h5>
      </div>
    </div>
  </div>
  <form id="add_form" method="post" enctype="multipart/form-data">
    <input type="hidden" name="form_submit" value="ok" />
    <input type="hidden" name="marketing_activity_id" value="<?=isset($_GET['marketing_activity_id']) ? $_GET['marketing_activity_id'] : 0?>" />
    <input type="hidden" name="goods_id" value="" />
    <input type="hidden" name="if_continue" value="0" />
    <div class="ncap-form-default">
      <dl class="row">
        <dt class="tit">
          <label for="goods_link"><em>*</em>商品链接</label>
        </dt>
        <dd class="opt">
          <input type="text" id="goods_link" name="goods_link" class="input-txt" >
          <a href="JavaScript:void(0);" class="ncap-btn-big ncap-btn-gray" id="get_goods_info"><span>获取商品信息</span></a>
          <span class="err"></span>
          <p class="notic"></p>
        </dd>
      </dl>
      <dl class="row">
        <dt class="tit">
          <label for="store_name"><em></em>店铺名</label>
        </dt>
        <dd class="opt">
          <input type="text" id="store_name" name="store_name" class="input-txt" disabled="disabled">
          <span class="err"></span>
          <p class="notic"></p>
        </dd>
      </dl>
      <dl class="row">
        <dt class="tit">
          <label for="goods_name"><em></em>商品名</label>
        </dt>
        <dd class="opt">
          <input type="text" id="goods_name" name="goods_name" class="input-txt" disabled="disabled" >
          <span class="err"></span>
          <p class="notic"></p>
        </dd>
      </dl>
      <dl class="row">
        <dt class="tit">
          <label for="goods_price"><em></em>商品原价</label>
        </dt>
        <dd class="opt">
          <input type="text" id="goods_price" name="goods_price" class="input-txt" disabled="disabled" >
          <span class="err"></span>
          <p class="notic"></p>
        </dd>
      </dl>
      <dl class="row">
        <dt class="tit">
          <label for="promotion_price"><em>*</em>
              <?php if($output['marketing_data']['marketing_activity_type'] == 3){?>
                  砍价价格
              <?php }elseif($output['marketing_data']['marketing_activity_type'] == 4){?>
                  拼团价格
              <?php }else{?>
                  商品特价
              <?php }?>
          </label>
        </dt>
        <dd class="opt">
          <input type="text" id="promotion_price" name="promotion_price" class="input-txt" >
          <span class="err"></span>
          <p class="notic"></p>
        </dd>
      </dl>
        <?php if($output['marketing_data']['marketing_activity_type'] < 3){?>
      <dl class="row">
        <dt class="tit">
          <label for="upper_limit"><em>*</em>每人最多购买数量</label>
        </dt>
        <dd class="opt">
          <input type="text" id="upper_limit" name="upper_limit" class="input-txt" >
          <span class="err"></span>
          <p class="notic"></p>
        </dd>
      </dl>
        <?php }else{?>
            <input type="hidden" id="upper_limit" name="upper_limit" class="input-txt" value="0">
        <?php }?>

      <div class="bot">
        <a href="JavaScript:void(0);" class="ncap-btn-big ncap-btn-green" id="submitBtnContinue"><span>提交并继续报名</span></a>
        <a href="JavaScript:void(0);" class="ncap-btn-big ncap-btn-green" id="submitBtn"><span><?php echo $lang['nc_submit'];?></span></a>
      </div>
    </div>
  </form>
</div>

<script>
//按钮先执行验证再提交表单
$(function(){
  $("#submitBtn,#submitBtnContinue").click(function(){
      var $_this = $(this);
      var $click_btn = $_this.attr('id');
      var $if_continue = ($click_btn === 'submitBtn') ? 0 : 1;
      $('input[name="if_continue"]').val($if_continue);
      if($("#add_form").valid()){
        $("#add_form").submit();
      }
  });
});

$(document).ready(function(){
  $("#add_form").validate({
    errorPlacement: function(error, element){
      var error_td = element.parents('dl').find('span.err');
            error_td.append(error);
        },
        rules : {
          goods_link: {
            required : true
          },
          promotion_price: {
            required : true
          },
          upper_limit: {
            required : true
          }
        },
        messages : {
          goods_link: {
            required : '<i class="fa fa-exclamation-circle"></i>请选择要添加的商品'
          },
          promotion_price: {
            required : '<i class="fa fa-exclamation-circle"></i>商品特价不能为空'
          },
          upper_limit: {
            required : '<i class="fa fa-exclamation-circle"></i>每人最多购买数量不能为空'
          }
        }
  });
});

/**
 * 点击获取商品信息
 */
$('#get_goods_info').click(function(){
    var $_goods_link = $('input[name="goods_link"]').val();
    if (is_empty($_goods_link) || !checkUrl($_goods_link)) {
        alert('请输入正确的商品链接');
        return false;
    }
    var $_parameter = getKeyValue($_goods_link);
    //判断参数中是否存在，goods_id参数
    var goods_id = ('goods_id' in $_parameter) ? $_parameter.goods_id : undefined;
    if (is_empty(goods_id)) {
      alert('url中获取不到goods_id');
      return false;
    }
    //请求商品信息
    var url = 'index.php?act=marketing_activity&op=get_goods_info';
    $.ajax({
            url:url,
            type:"get",
            data:{goods_id:goods_id,marketing_activity_id:<?=$_GET['marketing_activity_id']?>},
            dataType:"json",
            async:false,
            error:function(data){
                alert('未知错误！');
                return false;
            },
            success:function(data){
                var state = 'state' in data ? data.state : false;
                if (state !== true) {
                  var msg = 'msg' in data ? data.msg : '未知错误';
                  alert(msg);
                  return false;
                } else {
                  var datas = 'datas' in data ? data.datas : new Array();
                  //商品id
                  $('input[name="goods_id"]').val(datas.goods_id);
                  //店铺名
                  $('input[name="store_name"]').val(datas.store_name);
                  //商品名
                  $('input[name="goods_name"]').val(datas.goods_name);
                  //商品原价
                  $('input[name="goods_price"]').val(datas.goods_price);
                }
            }
        });

});

/**
* [is_empty 是否为空 -- undefined，null 都为空]
* @param  {[type]}  data [description]
* @return {Boolean}      [true-为空/false-不为空]
*/
function is_empty(data){
  data = $.trim(data);
  if((typeof(data) == 'undefined') || (data === null)){
    return true;
  }
  return (data.length == 0)?true:false;
}
/**
 * [checkUrl 检查url是否正确]
 * @return {[type]} [正确-true/错误-false]
 */
function checkUrl(url){
    var RegUrl = new RegExp();
    RegUrl.compile("^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+");
    if (!RegUrl.test(url)) {
        return false;
    }
    return true;
}

/**
 * 解析url中的参数
 * @param  string url
 * @return array
 */
function getKeyValue(url) {
   var result = {};
   //链接是否是短链接形式
   var reg = new RegExp('item-(\\d+)\.html', 'ig');
   var arr = reg.exec(url);
   if (!is_empty(arr)) {
     result['goods_id'] = arr[1];
   } else {
     var reg = new RegExp('([\?|&])(.+?)=([^&?]*)', 'ig');
     var arr = reg.exec(url);
     while (arr) {
      result[arr[2]] = arr[3];
      arr = reg.exec(url);
     }
   }
   return result;
}
</script>